unit EntityContainer;

interface

uses SysUtils, Criteria;

type

  TStringArray = array of String;
  TObjectArray = array of TObject;
  TIntegerArray = array of integer;

  TEntityContainer = class
  private
    fContent: TObjectArray;
  protected
    query: String;
  public
    constructor Create();
    destructor Destroy(); override;
    procedure setContent(aContent: TObjectArray);
    function getContent(): TObjectArray;
    function getLength(): integer;
    procedure releaseContent(); virtual;
    procedure addItem(aItem: TObject);
    function getItem(aIndex: integer): TObject;
    procedure removeItem(aIndex: integer; freeItem: boolean = true);
    procedure removeAll(freeItem: boolean = true); virtual;
  end;

implementation

uses
  Entity;

{ TEntityContainer }

function TEntityContainer.getLength(): integer;
begin
  Result := Length(fContent);
end;

procedure TEntityContainer.addItem(aItem: TObject);
begin
  SetLength(fContent, Length(fContent) + 1);
  fContent[High(fContent)] := aItem;
end;

constructor TEntityContainer.Create;
begin
  SetLength(fContent, 0);
end;

destructor TEntityContainer.Destroy;
begin
  releaseContent();
  inherited;
end;

function TEntityContainer.getContent: TObjectArray;
begin
  Result := fContent;
end;

function TEntityContainer.getItem(aIndex: integer): TObject;
begin
  if aIndex < Length(fContent) then
  begin
    Result := fContent[aIndex];
  end
  else
  begin
    Result := nil;
  end;
end;

procedure TEntityContainer.releaseContent;
var
  i: integer;
  tmpEntity: TObject;
  tmpCacheEntity: TObject;
begin
  for i := 0 to getLength() - 1 do
  begin
    fContent[i].Free;
    fContent[i] := nil;
  end;
  SetLength(fContent, 0);
  fContent := nil;
end;

procedure TEntityContainer.removeAll(freeItem: boolean = true);
var
  i: integer;
begin
  for i := getLength() - 1 downto 0 do
  begin
    removeItem(i, freeItem);
  end;
end;

procedure TEntityContainer.removeItem(aIndex: integer;
  freeItem: boolean = true);
var
  i: integer;
begin
  if (Length(fContent) > 0) then
  begin
    if freeItem then
    begin
      fContent[aIndex].Free;
    end;
    fContent[aIndex] := nil;
    if (Length(fContent) > 1) and (aIndex <> Length(fContent) - 1) then
    begin
      for i := aIndex to Length(fContent) - 2 do
      begin
        fContent[i] := fContent[i + 1];
      end;
    end;
    SetLength(fContent, Length(fContent) - 1);
  end;
end;

procedure TEntityContainer.setContent(aContent: TObjectArray);
begin
  fContent := aContent;
end;

end.
